<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Nginx 动态转发与重写指南</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        
        .card-hover {
            transition: all 0.3s ease;
        }
        
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
        }
        
        .code-block {
            background: #1e1e1e;
            color: #d4d4d4;
            border-radius: 12px;
            overflow: hidden;
            position: relative;
        }
        
        .code-header {
            background: #2d2d2d;
            padding: 8px 16px;
            font-size: 12px;
            color: #888;
            border-bottom: 1px solid #3e3e3e;
        }
        
        pre {
            margin: 0;
            padding: 20px;
            overflow-x: auto;
            font-family: 'Consolas', 'Monaco', monospace;
            line-height: 1.6;
        }
        
        .drop-cap {
            float: left;
            font-size: 4rem;
            line-height: 1;
            font-weight: 700;
            margin-right: 8px;
            margin-top: -8px;
            color: #667eea;
            font-family: 'Noto Serif SC', serif;
        }
        
        .section-divider {
            width: 60px;
            height: 4px;
            background: linear-gradient(90deg, #667eea, #764ba2);
            margin: 2rem auto;
            border-radius: 2px;
        }
        
        .mermaid {
            background: white;
            padding: 2rem;
            border-radius: 12px;
            box-shadow: 0 10px 30px rgba(0,0,0,0.1);
        }
        
        .highlight-box {
            background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
            color: white;
            padding: 1.5rem;
            border-radius: 12px;
            margin: 2rem 0;
            position: relative;
            overflow: hidden;
        }
        
        .highlight-box::before {
            content: '';
            position: absolute;
            top: -50%;
            right: -50%;
            width: 200%;
            height: 200%;
            background: radial-gradient(circle, rgba(255,255,255,0.1) 0%, transparent 70%);
            animation: pulse 3s ease-in-out infinite;
        }
        
        @keyframes pulse {
            0%, 100% { transform: scale(1); opacity: 0.5; }
            50% { transform: scale(1.1); opacity: 0.3; }
        }
        
        .concept-card {
            background: white;
            border-radius: 16px;
            padding: 2rem;
            box-shadow: 0 10px 30px rgba(0,0,0,0.08);
            border: 1px solid rgba(102, 126, 234, 0.1);
            position: relative;
            overflow: hidden;
        }
        
        .concept-card::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            width: 4px;
            height: 100%;
            background: linear-gradient(180deg, #667eea, #764ba2);
        }
        
        .icon-circle {
            width: 60px;
            height: 60px;
            background: linear-gradient(135deg, #667eea, #764ba2);
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
            font-size: 24px;
            margin-bottom: 1rem;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <div class="hero-gradient text-white">
        <div class="container mx-auto px-6 py-20">
            <div class="max-w-4xl mx-auto text-center">
                <h1 class="text-5xl md:text-6xl font-bold mb-6 leading-tight">
                    Nginx 动态转发与重写
                </h1>
                <p class="text-xl md:text-2xl opacity-90 mb-8">
                    掌握请求路由的艺术，构建灵活高效的 Web 架构
                </p>
                <div class="flex justify-center space-x-4">
                    <span class="inline-flex items-center px-4 py-2 bg-white bg-opacity-20 rounded-full">
                        <i class="fas fa-server mr-2"></i> 动态转发
                    </span>
                    <span class="inline-flex items-center px-4 py-2 bg-white bg-opacity-20 rounded-full">
                        <i class="fas fa-exchange-alt mr-2"></i> URL 重写
                    </span>
                    <span class="inline-flex items-center px-4 py-2 bg-white bg-opacity-20 rounded-full">
                        <i class="fas fa-code mr-2"></i> 高级配置
                    </span>
                </div>
            </div>
        </div>
    </div>

    <!-- Main Content -->
    <div class="container mx-auto px-6 py-12">
        <div class="max-w-5xl mx-auto">
            
            <!-- Introduction -->
            <div class="bg-white rounded-2xl shadow-xl p-8 mb-12">
                <p class="text-lg leading-relaxed text-gray-700">
                    <span class="drop-cap">在</span>Nginx 中实现请求的动态转发和重写，可以通过配置 <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">location</code> 块中的 <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">rewrite</code> 指令和 <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">proxy_pass</code> 指令来完成。这些功能使得 Nginx 可以根据不同的条件动态地转发请求到不同的后端服务或修改请求的 URL。
                </p>
            </div>

            <!-- Core Concepts -->
            <div class="mb-16">
                <h2 class="text-3xl font-bold text-gray-800 mb-8 text-center">
                    <i class="fas fa-lightbulb text-purple-600 mr-3"></i>核心概念
                </h2>
                <div class="grid md:grid-cols-2 gap-6">
                    <div class="concept-card card-hover">
                        <div class="icon-circle">
                            <i class="fas fa-directions"></i>
                        </div>
                        <h3 class="text-xl font-bold text-gray-800 mb-3">动态转发</h3>
                        <p class="text-gray-600">根据请求的内容或其他条件将请求转发到不同的后端服务器，实现灵活的请求路由策略。</p>
                    </div>
                    <div class="concept-card card-hover">
                        <div class="icon-circle">
                            <i class="fas fa-edit"></i>
                        </div>
                        <h3 class="text-xl font-bold text-gray-800 mb-3">URL 重写</h3>
                        <p class="text-gray-600">修改请求的 URL，通常用于改变请求路径或查询参数，以便请求可以正确地被处理。</p>
                    </div>
                </div>
            </div>

            <!-- Visualization -->
            <div class="mb-16">
                <h2 class="text-3xl font-bold text-gray-800 mb-8 text-center">
                    <i class="fas fa-project-diagram text-purple-600 mr-3"></i>工作流程图
                </h2>
                <div class="mermaid">
                    graph LR
                        A[客户端请求] --> B{Nginx}
                        B --> C{URL 匹配}
                        C -->|匹配规则1| D[重写 URL]
                        C -->|匹配规则2| E[条件判断]
                        D --> F[动态转发]
                        E -->|条件1| G[后端服务1]
                        E -->|条件2| H[后端服务2]
                        F --> I[代理传递]
                        I --> G
                        I --> H
                        style A fill:#f9f,stroke:#333,stroke-width:2px
                        style B fill:#bbf,stroke:#333,stroke-width:2px
                        style G fill:#bfb,stroke:#333,stroke-width:2px
                        style H fill:#bfb,stroke:#333,stroke-width:2px
                </div>
            </div>

            <!-- Configuration Examples -->
            <div class="mb-16">
                <h2 class="text-3xl font-bold text-gray-800 mb-8">
                    <i class="fas fa-cog text-purple-600 mr-3"></i>配置实践
                </h2>

                <!-- Dynamic Forwarding -->
                <div class="mb-12">
                    <h3 class="text-2xl font-semibold text-gray-800 mb-6 flex items-center">
                        <span class="bg-purple-600 text-white rounded-full w-8 h-8 flex items-center justify-center mr-3 text-sm">1</span>
                        动态转发配置
                    </h3>
                    <p class="text-gray-600 mb-6 leading-relaxed">
                        动态转发涉及根据请求的 URL 或其他请求属性，将请求转发到不同的后端服务。可以使用 Nginx 的 <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">proxy_pass</code> 指令进行动态转发。
                    </p>
                    <div class="code-block">
                        <div class="code-header">
                            <i class="fas fa-code mr-2"></i>nginx.conf
                        </div>
                        <pre><code>server {
    listen 80;
    server_name demo.com;

    location /api/ {
        # 动态转发到不同的后端服务
        if ($arg_service = 'service1') {
            proxy_pass http://backend_service1;
        }
        if ($arg_service = 'service2') {
            proxy_pass http://backend_service2;
        }
    }
}</code></pre>
                    </div>
                    <div class="mt-6 bg-blue-50 border-l-4 border-blue-500 p-4 rounded">
                        <p class="text-sm text-blue-800">
                            <i class="fas fa-info-circle mr-2"></i>
                            <strong>关键点：</strong>使用 <code class="bg-blue-100 px-1">$arg_service</code> 变量根据查询参数动态选择后端服务。
                        </p>
                    </div>
                </div>

                <!-- URL Rewriting -->
                <div class="mb-12">
                    <h3 class="text-2xl font-semibold text-gray-800 mb-6 flex items-center">
                        <span class="bg-purple-600 text-white rounded-full w-8 h-8 flex items-center justify-center mr-3 text-sm">2</span>
                        URL 重写配置
                    </h3>
                    <p class="text-gray-600 mb-6 leading-relaxed">
                        URL 重写用于修改请求的 URL。使用 <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">rewrite</code> 指令可以将请求的 URL 修改为新的格式，并重新定向到相应的处理逻辑。
                    </p>
                    <div class="code-block">
                        <div class="code-header">
                            <i class="fas fa-code mr-2"></i>nginx.conf
                        </div>
                        <pre><code>server {
    listen 80;
    server_name demo.com;

    location / {
        # 将 /old-path/ 重写为 /new-path/
        rewrite ^/old-path/(.*)$ /new-path/$1 last;
        
        # 动态转发到后端服务
        proxy_pass http://backend;
    }
}</code></pre>
                    </div>
                </div>

                <!-- Complex Configuration -->
                <div class="mb-12">
                    <h3 class="text-2xl font-semibold text-gray-800 mb-6 flex items-center">
                        <span class="bg-purple-600 text-white rounded-full w-8 h-8 flex items-center justify-center mr-3 text-sm">3</span>
                        复杂场景配置
                    </h3>
                    <p class="text-gray-600 mb-6 leading-relaxed">
                        在更复杂的场景下，您可能需要结合使用 <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">rewrite</code> 指令和条件判断来实现更细粒度的控制。
                    </p>
                    <div class="code-block">
                        <div class="code-header">
                            <i class="fas fa-code mr-2"></i>nginx.conf
                        </div>